﻿using System;
using System.Collections.Generic;

namespace ProblemsSet
{
    public class Problem_37 : BaseProblem
    {
        public override object GetResult()
        {
            var val = new HashSet<long>(MathLogic.GetPrimeList(100000));
            var cnt = 0;
            long summ = 0;
            foreach (var l in val)
            {
                if (l < 10) continue;
                var exit = false;

                var tmp = l/10;
                while (tmp > 0)
                {
                    if (!val.Contains(tmp))
                    {
                        exit = true;
                        break;
                    }
                    tmp /= 10;
                }
                if (exit) continue;
                tmp = l % ((int)Math.Pow(10, (int)(Math.Log10(l))));
                while (tmp > 0)
                {
                    if (!val.Contains(tmp))
                    {
                        exit = true;
                        break;
                    }
                    tmp = tmp % ((int)Math.Pow(10, (int)(Math.Log10(tmp))));
                }
                if (exit) continue;
                summ += l;
                cnt++;
                if (cnt == 11) break;
            }
            return summ;
        }

        public override string Problem
        {
            get
            {
                return @"The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.

Find the sum of the only eleven primes that are both truncatable from left to right and right to left.

NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.";
            }
        }

        public override bool IsSolved
        {
            get
            {
                return true;
            }
        }

        public override object Answer
        {
            get
            {
                return 748317;
            }
        }

    }
}
